/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/13
 * Time: 16:33
 * Description:
 */
package com.hangkai.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hangkai.reggie.common.BaseContext;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.dao.AddressBookDao;
import com.hangkai.reggie.domain.AddressBook;
import com.hangkai.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加用户地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<String> addAddress(@RequestBody AddressBook addressBook){
        //注意 要保存的地址 要绑定用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success("添加地址成功");
    }

    /**
     * 列出本用户的地址簿
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> getListAddress(){
        //根据用户id查到的地址进行展示
        Long userId = BaseContext.getCurrentId();
        //条件构造器
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
//        lqw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(lqw);
        return Result.success(list);
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result<AddressBook> setDefaultAddress(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        luw.set(AddressBook::getIsDefault, 0);
        //update address_book set is_default=0 where user_id=?
        addressBookService.update(luw);

        addressBook.setIsDefault(1);
        //update address_book set is_default=1 where id=?
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 提交订单会获取默认地址来显示
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        //获得当前用户的默认地址
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        lqw.eq(AddressBook::getIsDefault, 1);
        //select * from address_book where user_id=? and is_default=1
        AddressBook addressBookServiceOne = addressBookService.getOne(lqw);
        return Result.success(addressBookServiceOne);
    }

    /**
     * 修改地址信息
     * @return
     */
    @PutMapping
    public Result<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success("修改成功");
    }

    /**
     * 根据id删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> deleteAddress(Long ids){
        addressBookService.removeById(ids);
        return Result.success("删除地址成功");
    }
    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getByid(@PathVariable Long id){
        AddressBook address = addressBookService.getById(id);
        if(address != null){
            return Result.success(address);
        }else {
            return Result.error("没有找到该对象");
        }

    }

}
